{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# Example: Static inverse free-boundary equilibrium calculations (in KSTAR)\n", "\n", "---\n", "\n", "Here we generate a KSTAR-like equilibrium using the machine geometry in the [FUSE.jl repository](https://github.com/ProjectTorreyPines/FUSE.jl).\n", "\n", "The equilibrium/profile parameters are a mixture of number from the source files and **completely made up**; please experiment on your own and change them to more realistic values as you please!\n", "\n", "Note that there are no passive conducting structures in this description!" ] }, { "cell_type": "markdown", "id": "1", "metadata": {}, "source": [ "### Import packages" ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "import numpy as np" ] }, { "cell_type": "markdown", "id": "3", "metadata": {}, "source": [ "### Create the machine object" ] }, { "cell_type": "code", "execution_count": null, "id": "4", "metadata": {}, "outputs": [], "source": [ "# build machine\n", "\n", "from freegsnke import build_machine\n", "\n", "tokamak = build_machine.tokamak(\n", " active_coils_path=\"../machine_configs/KSTAR/KSTAR_active_coils.pickle\",\n", " limiter_path=\"../machine_configs/KSTAR/KSTAR_limiter.pickle\",\n", " wall_path=\"../machine_configs/KSTAR/KSTAR_wall.pickle\",\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "5", "metadata": {}, "outputs": [], "source": [ "# plot the machine\n", "\n", "fig, ax = plt.subplots(figsize=(8, 8), dpi=80)\n", "\n", "tokamak.plot(axis=ax, show=False) # plots the active coils\n", "ax.plot(tokamak.limiter.R, tokamak.limiter.Z, \"k-\", linewidth=1.2) # plots the limiter\n", "\n", "ax.grid(alpha=0.5)\n", "ax.set_aspect(\"equal\")\n", "ax.set_xlim(0.4, 3.9)\n", "ax.set_ylim(-2.6, 2.6)\n", "ax.set_xlabel(r\"Major radius, $R$ [m]\")\n", "ax.set_ylabel(r\"Height, $Z$ [m]\")" ] }, { "cell_type": "markdown", "id": "6", "metadata": {}, "source": [ "### Instantiate an equilibrium" ] }, { "cell_type": "code", "execution_count": null, "id": "7", "metadata": {}, "outputs": [], "source": [ "from freegsnke import equilibrium_update\n", "\n", "eq = equilibrium_update.Equilibrium(\n", " tokamak=tokamak, # provide tokamak object\n", " Rmin=1.2, Rmax=2.7, # radial range\n", " Zmin=-1.5, Zmax=1.5, # vertical range\n", " nx=129, # number of radial grid points (needs to be of the form 2**n + 1)\n", " ny=129, # number of vertical grid points (needs to be of the form 2**n + 1)\n", ")" ] }, { "cell_type": "markdown", "id": "8", "metadata": {}, "source": [ "### Instantiate a profile object" ] }, { "cell_type": "code", "execution_count": null, "id": "9", "metadata": {}, "outputs": [], "source": [ "# initialise the profiles\n", "\n", "from freegsnke.jtor_update import ConstrainPaxisIp\n", "\n", "profiles = ConstrainPaxisIp(\n", " eq=eq, # equilibrium object\n", " paxis=5.0e4, # illustrative pressure on axis [Pa]\n", " Ip=0.8e6, # plasma current from the FUSE KSTAR case [A]\n", " fvac=3.24, # abs(B0) * R0 from the FUSE KSTAR case [T m]\n", " alpha_m=1.8, # profile function parameter\n", " alpha_n=1.2, # profile function parameter\n", ")" ] }, { "cell_type": "markdown", "id": "10", "metadata": {}, "source": [ "### Load the static nonlinear solver" ] }, { "cell_type": "code", "execution_count": null, "id": "11", "metadata": {}, "outputs": [], "source": [ "from freegsnke import GSstaticsolver\n", "\n", "GSStaticSolver = GSstaticsolver.NKGSsolver(eq, seed=42)" ] }, { "cell_type": "markdown", "id": "12", "metadata": {}, "source": [ "### Constraints" ] }, { "cell_type": "code", "execution_count": null, "id": "13", "metadata": {}, "outputs": [], "source": [ "# set the desired lower X-point and magnetic-axis locations\n", "Rx, Zx = 1.40, -1.05\n", "null_points = [[Rx,Rx], [Zx, -Zx]]\n", "\n", "# constrain boundary points to the same flux surface\n", "isoflux_set = np.array([[\n", " [1.31, 2.23, Rx, Rx],\n", " [0.0, 0.0, Zx, -Zx],\n", "]])\n", "\n", "from freegsnke.inverse import Inverse_optimizer\n", "\n", "constrain = Inverse_optimizer(\n", " null_points=null_points,\n", " isoflux_set=isoflux_set,\n", ")" ] }, { "cell_type": "markdown", "id": "14", "metadata": {}, "source": [ "### The inverse solve" ] }, { "cell_type": "code", "execution_count": null, "id": "15", "metadata": {}, "outputs": [], "source": [ "GSStaticSolver.inverse_solve(\n", " eq=eq,\n", " profiles=profiles,\n", " constrain=constrain,\n", " target_relative_tolerance=1e-5,\n", " target_relative_psit_update=1e-3,\n", " max_solving_iterations=100,\n", " l2_reg=np.array([1e-14] * 14),\n", " Picard_handover=1e-4,\n", " force_up_down_symmetric=True,\n", " verbose=True, \n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "16", "metadata": {}, "outputs": [], "source": [ "# refine with a forward solve (now the currents are known)\n", "GSStaticSolver.solve(eq=eq, \n", " profiles=profiles, \n", " constrain=None, \n", " target_relative_tolerance=1e-9,\n", " verbose=False, # suppress output\n", " )" ] }, { "cell_type": "code", "execution_count": null, "id": "17", "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots(figsize=(8, 8), dpi=80)\n", "\n", "ax.grid(zorder=0, alpha=0.75)\n", "ax.set_aspect(\"equal\")\n", "eq.tokamak.plot(axis=ax, show=False) # plots the active coils\n", "eq.plot(axis=ax, show=False) # plots the equilibrium\n", "constrain.plot(axis=ax, show=False) # plots the constraints\n", "ax.plot(tokamak.limiter.R, tokamak.limiter.Z, \"k-\", linewidth=1.2) # plots the limiter\n", "# ax.set_xlim(1.15, 2.4)\n", "# ax.set_ylim(-1.55, 1.55)\n", "ax.set_xlabel(r\"Major radius, $R$ [m]\")\n", "ax.set_ylabel(r\"Height, $Z$ [m]\")" ] }, { "cell_type": "code", "execution_count": null, "id": "18", "metadata": {}, "outputs": [], "source": [ "eq.tokamak.getCurrents()" ] } ], "metadata": { "kernelspec": { "display_name": "freegsnke", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.20" } }, "nbformat": 4, "nbformat_minor": 5 }